home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13696 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  72 lines

  1. Path: kuhub.cc.ukans.edu!anh
  2. From: anh@kuhub.cc.ukans.edu (TRAN PHAN ANH)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: reversing a string
  5. Message-ID: <1996Apr9.144011.117444@kuhub.cc.ukans.edu>
  6. Date: 9 Apr 96 14:40:11 CDT
  7. References: <4k6cjl$j8f@central.server.swt.edu> <4kb1s7$6eu@ibm32.perftech.com> <829058514snz@genesis.demon.co.uk>
  8. Organization: University of Kansas Academic Computing Services
  9.  
  10. Something like this?  Or is the s2 argument considered an extra variable.
  11.  
  12. Anh
  13.  
  14.  
  15. #include <stdio.h>
  16. #include <string.h>
  17.  
  18. void reverse(char *s1, char *s2)
  19. {
  20.   if(s1>=s2)
  21.     return;
  22.  
  23.   *s1^=*s2;
  24.   *s2^=*s1;
  25.   *s1^=*s2;
  26.  
  27.   reverse(s1+1,s2-1);
  28. }
  29.  
  30.  
  31. int main(int argc, char *argv[])
  32. {
  33.   printf("Arg: %s\n",argv[1]);
  34.  
  35.   reverse(argv[1],&argv[1][strlen(argv[1])-1]);
  36.  
  37.   printf("Result: %s\n",argv[1]);
  38. }
  39.  
  40.  
  41.  
  42.  
  43.  
  44. In article <829058514snz@genesis.demon.co.uk>, Lawrence Kirby <fred@genesis.demon.co.uk> writes:
  45. > In article <4kb1s7$6eu@ibm32.perftech.com>
  46. >            murf@perftech.com "John Murphy" writes:
  47. >>In article <4k6cjl$j8f@central.server.swt.edu>, ln16674@nyssa.swt.edu 
  48. >>says...
  49. >>>
  50. >>>I have a challenge from a friend of mine.  He wanted me to reverse a string 
  51. >>>with recursion without using any additional variables or loops.  I got mine
  52. >>>to work by using exclusive or, but I needed an additional variable.  Can 
  53. >>>someone help with this problem without using the additional variable?
  54. >>>
  55. >>>Thanks
  56. >>You can swap two variables, x and y, with the following series of exclusive 
  57. >>or's:
  58. >>        x ^= y;
  59. >>        y ^= x;
  60. >>        x ^= y;
  61. > From what he wrote I believe Leland was already doing that. The problem is
  62. > to write the entire function without using an additional variable. It
  63. > can be done! :-)
  64. > -- 
  65. > -----------------------------------------
  66. > Lawrence Kirby | fred@genesis.demon.co.uk
  67. > Wilts, England | 70734.126@compuserve.com
  68. > -----------------------------------------
  69.